﻿using AutoMapper;
using Logistics.EntityFrameworkCore;
using Logistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Logistics.Contract
{
    public class CarryContractAppService : ApplicationService, ICarryContractAppService
    {
        private readonly ICarryContractRepository _carry;
        private readonly IRepository<Carry, int> _carryRepository;
        private readonly LogisticsDbContext db;
        public CarryContractAppService(ICarryContractRepository carry, IRepository<Carry, int> carryRepository, LogisticsDbContext _db)
        {
            _carry= carry;
            _carryRepository= carryRepository;
            db= _db;
        }
        public async Task<CarryDto> Create(CarryDto ca)
        {
            var carr = new Carry
            {
                NumberId = ca.NumberId,
                ContractTitle = ca.ContractTitle,
                OutsourcingUnit = ca.OutsourcingUnit,
                OutsourcingLeader = ca.OutsourcingLeader,
                Line = ca.Line,
                Freightrate = ca.Freightrate,
                CharterConditions = ca.CharterConditions,
                CharterAmount = ca.CharterAmount,
                SigningDate = Convert.ToDateTime(ca.SigningDate),
                Handler = ca.Handler,
                ProjectDescription = ca.ProjectDescription,
                ChangeClause = ca.ChangeClause,
                Enclosure = ca.Enclosure
             };

            await _carry.Create(carr);
            return ObjectMapper.Map<Carry, CarryDto>(carr);
        }

        public async Task<int> Delete(int Id)
        {
            var result= await _carry.Delete(Id);
            return result;
        }

        public async Task<CarryDto> GetCarryById(int Id)
        { 
            var student = await _carry.GetCarryById(Id);
            return ObjectMapper.Map<Carry, CarryDto>(student);
        }

        public async Task<PagedResultDto<CarryDto>> GetCarrys(CarrySearchDto csd)
        {
            //var all = _carryRepository.ToList();
            //var listr = _carry.GetCarrys();
            var carryList = await _carryRepository.GetPagedListAsync(
                csd.SkipCount,
                csd.MaxResultCount,
                csd.Sorting);
            if (!string.IsNullOrEmpty(csd.Title))
            {
                carryList.Where(m => m.ContractTitle.Contains(csd.Title)).ToList();
            }
            if (!string.IsNullOrEmpty(csd.Time))
            {
                 carryList.Where(m => m.CreationTime == (Convert.ToDateTime(csd.Time))).ToList();
            }
            var totalCount = await AsyncExecuter.CountAsync(
                 _carryRepository.WhereIf(
                     !csd.Title.IsNullOrWhiteSpace(),
                     m => m.ContractTitle.Contains(csd.Title)
                 )
             );

            return new PagedResultDto<CarryDto>(
              totalCount,
              ObjectMapper.Map<List<Carry>, List<CarryDto>>(carryList)
          );
        }
        public async Task<PagedResultDto<CarryDto>> GetDapperCarrys(CarrySearchDto csd)
        {
            var carr = db.Carry.ToList();
            var file = db.Field.ToList();
            var lin = db.Line.ToList();
            var own = db.Owner.ToList();
            var list = from a in carr
                       join b in own on a.OutsourcingLeader equals b.Id
                       join c in file on a.OutsourcingUnit equals c.Id
                       join d in lin on a.Line equals d.Id
                       select new CarryDto
                       {
                           Id=a.Id,
                           NumberId=a.NumberId,
                           ContractTitle=a.ContractTitle,
                           OutsourcingUnit=a.OutsourcingUnit,
                           OutsourcingLeader=a.OutsourcingLeader,
                           Line=a.Line,
                           Freightrate=a.Freightrate,
                           State = a.State,
                           CharterConditions =a.CharterConditions,
                           CharterAmount=a.CharterAmount,
                           SigningDate=a.SigningDate,
                           Handler=a.Handler,
                           ProjectDescription=a.ProjectDescription,
                           ChangeClause=a.ChangeClause,
                           Enclosure=a.Enclosure,
                           CreationTime=a.CreationTime,
                           OName = b.Name,
                           FName = c.Name,
                           LName = d.Name,
                       };
            if (!string.IsNullOrEmpty(csd.Title))
            {
                list=list.Where(m => m.ContractTitle.Contains(csd.Title));
            }
            var totalCount = list.Count();
            list = list.Skip(csd.SkipCount).Take(csd.MaxResultCount);
            return new PagedResultDto<CarryDto>(
              totalCount,
              list.ToList()
          );
        }
        public async Task<int> Update(CarryDto ca)
        {
            var carr = await _carryRepository.GetAsync(ca.Id);
            carr.ContractTitle = ca.ContractTitle;
            carr.OutsourcingUnit = ca.OutsourcingUnit;
            carr.OutsourcingLeader = ca.OutsourcingLeader;
            carr.Line = ca.Line;
            carr.Freightrate = ca.Freightrate;
            carr.CharterConditions = ca.CharterConditions;
            carr.CharterAmount = ca.CharterAmount;
            carr.SigningDate = Convert.ToDateTime(ca.SigningDate);
            carr.Handler = ca.Handler;
            carr.ProjectDescription = ca.ProjectDescription;
            carr.ChangeClause = ca.ChangeClause;
            carr.Enclosure = ca.Enclosure;
            await _carryRepository.UpdateAsync(carr);
            return 1;
        }
    }
}
